package com.youmingxing.dialog;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.activity.UploadActivity;
import com.youmingxing.utils.Util;

import java.util.ArrayList;

import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

/**
 * JHS
 */

public class NewWorkDialog extends BaseActivity implements OnClickListener {

    static int VIDEO_PICK = 0;
    static int PHOTO_PICK = 1;
    static int TAKE_PHOTO = 2;
    static int UPLOAD = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_camera_selection);

        final DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        final WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = layoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;

        getWindow().setAttributes(layoutParams);
        initView();
    }


    private void initView() {
        findViewById(R.id.btn_video).setOnClickListener(this);
        findViewById(R.id.btn_gallery).setOnClickListener(this);
        findViewById(R.id.btn_take_pic).setOnClickListener(this);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_video:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*");
                startActivityForResult(intent, VIDEO_PICK);
                break;
            case R.id.btn_gallery:
                PhotoPickerIntent intentPP = new PhotoPickerIntent(this);
                intentPP.setPhotoCount(1);
                intentPP.setShowCamera(false);
                intentPP.setShowGif(true);
                startActivityForResult(intentPP, PHOTO_PICK);
                break;
            case R.id.btn_take_pic:
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";
                startActivityForResult(intent, TAKE_PHOTO);
                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            if (resultCode == Activity.RESULT_OK && requestCode != UPLOAD) {
                Intent intent = new Intent();
                intent.setClass(this, UploadActivity.class);
                if (requestCode == TAKE_PHOTO) {
                    Bundle bundle = data.getExtras();
                    //获取相机返回的数据，并转换为图片格式
                    Bitmap bitmap = (Bitmap)bundle.get("data");
                    String path = Util.saveImageToGallery(NewWorkDialog.this,bitmap);
                    intent.putExtra("type", PHOTO_PICK);
                    ArrayList<String> photo = new ArrayList<>();
                    photo.add(path);
                    intent.putExtra("photo_array", photo);
                } else if (requestCode == VIDEO_PICK) {
                    try {
                        Uri selectedImage = data.getData();
                        String str = selectedImage.toString();
                        String filePath;
                        if (str.startsWith("file")){
                            //返回的是文件地址
                            filePath = str.replace("file://","");
                        } else {
//                        //返回的是uri类型
//                        Uri selectedImage = data.getData();
                            String[] filePathColumn = {MediaStore.Images.Media.DATA};
                            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                            cursor.moveToFirst();
                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            filePath = cursor.getString(columnIndex);
                            cursor.close();

                        }
                        intent.putExtra("type", VIDEO_PICK);
                        intent.putExtra("path", filePath);
                    } catch (Exception e) {
                        Log.e("Demo", "choose file error!", e);
                    }
                } else if (requestCode == PHOTO_PICK) {
                    ArrayList<String> photos =
                            data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                    intent.putExtra("type", PHOTO_PICK);
                    intent.putExtra("photo_array", photos);
                }
                startActivityForResult(intent, UPLOAD);
            }
            if (resultCode == Activity.RESULT_OK && requestCode == UPLOAD) {
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
